/**
 * 
 */
package me.twoday.library.mine.common.network;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import me.twoday.library.mine.common.LoggerUtil;

import org.apache.http.client.methods.HttpGet;

/**
 * HTTP GET 요청에 사용되는 클래스
 * 
 * @author 박우람
 * @since 2011.02.13
 */
public class HttpGetConnection extends HttpConnection
{
	private static final String TAG = "HttpGetConnection";
	private final LoggerUtil mLog;
	private String mParams;
	
	/**
	 * 기본 생성자
	 * 
	 * @param httpHandler 처리 핸들러
	 */
	public HttpGetConnection(HttpHandler httpHandler)
	{
		super(httpHandler);
		this.mParams = "?";
		this.mLog = LoggerUtil.getInstance(TAG);
	}
	
	/**
	 * 문자열 형태의 파라미터를 입력합니다.
	 * 
	 * @param name 파라미터명
	 * @param value 파라미터값
	 */
	public void putParam(String name, String value)
	{
		try
		{
			String encodedValue = URLEncoder.encode(value, "UTF-8");
			this.mParams = mParams + name + "=" + encodedValue + "&";
		}
		catch(UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * 정수 형태의 파라미터를 입력합니다.
	 * 
	 * @param name 파라미터명
	 * @param value 파라미터값
	 */
	public void putParam(String name, int value)
	{
		try
		{
			String encodedValue = URLEncoder.encode(Integer.toString(value), "UTF-8");
			this.mParams = mParams + name + "=" + encodedValue + "&";
		}
		catch(UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see com.starpl.platform.common.network.HttpConnection#run()
	 */
	@Override
	public void run()
	{
		try
		{
			executePreProcess();
			HttpGet httpGet = new HttpGet(mUrl + mParams);
			httpGet.setHeader("Cookie", getCookieSessionKey());
			mLog.d("Http Request(GET): " + mUrl + mParams);
			
			boolean hasEntity = executePostProcess(mHttpClient.execute(httpGet));
			
			if(hasEntity)
			{
				httpGet.abort();
			}
		}
		catch(Exception e)
		{
			processNetworkError(e);
		}
	}
}
